package com.shangxia.monomer.study.math.algorithm.sort;

import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouya38158
 * @Package com.shangxia.monomer.study.math.algorithm.sort
 * @date 2021/7/21 15:30
 */
public class Selectionsort {
    public static void main(String[] args) {
        int[] arr = {2,3,5,7,9,4,1,8,6};
        System.out.println(JSONObject.toJSONString(arr));
        selectionSort(arr);
        System.out.println(JSONObject.toJSONString(arr));
    }

    public static void selectionSort(int[] arr) {
        int len = arr.length;

        for (int i = 0; i < len - 1; i++) {
            int minIndex = i;

            for (int j = i + 1; j < len; j++) {
                if (arr[j] < arr[minIndex]){
                    minIndex = j;
                }
            }

            int tmp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = tmp;
        }
    }
}
